package com.bjpowernode.springboot.controller;

import com.bjpowernode.springboot.entity.BookBean;
import com.bjpowernode.springboot.entity.PageBean;
import com.bjpowernode.springboot.service.BookService;
import com.bjpowernode.springboot.service.PageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 主要实现分页和分页跳转控制层
 */
@RequestMapping("/page")
@Controller
public class PageController {

    @Autowired
    private PageService pageService; // 操作分页

    @Autowired
    private BookService bookService; // 操作t_user

    @RequestMapping("/do-page-nav")
    public ModelAndView pageNavgation(HttpServletRequest request) {
        int count = bookService.countAllBooks(); // 查询当前的总数据量
        int start = Integer.parseInt(request.getParameter("start"));
        ModelAndView mv = new ModelAndView();
        PageBean pageBean = new PageBean(start);
        if (start + pageBean.getOffset() > count) {
            mv.setViewName("show-data-null");
        }
        List<BookBean> bookBeanList = bookService.queryBookByInterval(pageBean);
        mv.addObject("bookLists",bookBeanList);
        mv.setViewName("show-data-test-disabled" + (start % 6));
        return mv;
    }

}
